from utils.utils import breathing_animation 
import pandas as pd
class Pet:
    def __init__(self, position = (0, 0), size = (0, 0), name = 'pet', level = 1, hunger = 100.0, mood = 100.0, clean = 100.0, health = 100.0, now_exp = 0):
        self.x, self.y = position
        self.width, self.height = size
        self.breath_x = self.x
        self.breath_y = self.y
        self.breath_width = self.width
        self.breath_height = self.height
        self.width_size = 1.0
        self.height_size = 1.0
        self.pet_name = name
        self.level = level
        self.hunger = hunger
        self.mood = mood
        self.clean = clean
        self.health = health
        self.now_exp = now_exp
    
    def breath(self):
        self.breath_x = self.x - (self.width * self.width_size - self.width)/2
        self.breath_y = self.y - (self.height * self.height_size - self.height)
        self.breath_width = (self.width * self.width_size)
        self.breath_height = (self.height * self.height_size)
    
    def breath_(self, index = 0.01):
        self.width_size = breathing_animation(index/30, W=0.05)*3-2
        self.height_size = breathing_animation(index/30, W=0.05)*2-1
        
    def load_data(self, file_path="v-pet./data./player_data.csv"):
        try:
            data = pd.read_csv(file_path)
            row = data.iloc[0]
            #self.x, self.y = row['position']
            #self.width, self.height = row['size']
            self.pet_name = row['name']
            self.level = row['level']
            self.hunger = row['hunger']
            self.mood = row['mood']
            self.clean = row['clean']
            self.health = row['health']
            self.now_exp = row['now_exp']
            return True
        
        except Exception as e:
            print("加载宠物数据异常:\n"+str(e))
            return False
        
    def save_data(self, file_path="v-pet./data./pet_data.csv"):
        pet_data = {
            'name': [self.pet_name],
            'level': [self.level],
            'hunger': [self.hunger],
            'mood': [self.mood],
            'clean': [self.clean],
            'health': [self.health],
            'now_exp': [self.now_exp]
        }
        df = pd.DataFrame(pet_data)
        df.to_csv(file_path, index=True)
        print("宠物数据已保存到", file_path)